package com.beemans.corekit.ui.dialog;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.beemans.corekit.R;
import com.beemans.corekit.databinding.DialogLoadingBinding;
import com.beemans.corekit.integration.toolkit.ActivityUtils;
import com.beemans.corekit.integration.toolkit.BarUtils;

public class LoadingDialog extends BaseDialogFragment<DialogLoadingBinding> {
    private static BaseDialogFragment<?> loadingDialog = null;

    @Override
    public int getTheme() {
        return R.style.LoadingDialogStyle;
    }

    @Nullable
    @Override
    protected DialogLoadingBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return DialogLoadingBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        setCancelable(false);
        BarUtils.hideNavBar(this);
    }

    public static void showLoading() {
        if (loadingDialog == null) {
            final Activity topActivity = ActivityUtils.currentActivity();
            if (!(topActivity instanceof FragmentActivity)) {
                return;
            }
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }

            loadingDialog = new LoadingDialog().init((FragmentActivity) topActivity);
            loadingDialog.show();
        }
    }

    public static void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }
}